package com.xxwy.segmenttree.practice.leetcode;

/**
 * @author xxwy
 */
public class NumArray2 {

    //sun[i] 存储前i个元素的和
    private int[] sum;

    public NumArray2(int[] nums) {
        sum = new int[nums.length + 1];
        sum[0] = 0;
        for (int i = 1; i < sum.length; i++) {
            sum[i] = sum[i-1] + nums[i-1];
        }
    }

    public int sumRange(int i, int j) {
        return sum[j+1] - sum[i];
    }

    public static void main(String[] args) {
        int[] ints = {-2, 0, 3, -5, 2, -1};
        NumArray2 numArray2 = new NumArray2(ints);
        System.out.println(numArray2.sumRange(0, 5));
    }

}
